package com.source.module_kotlin_library.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.source.module_kotlin_library.R

class WorldAdapter : RecyclerView.Adapter<WorldAdapter.ViewHolder> {
    var context: Context
    var dataList = ArrayList<String>()
    var myItemClickListener: MyItemClickListener? = null

    constructor(context: Context, dataList: ArrayList<String>) : super() {
        this.context = context
        this.dataList = dataList
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_world_list_content, null)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = dataList[position]
        holder.tvContent.text = data

        holder.tvContent.setOnClickListener {
            myItemClickListener?.onItemClick(data)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }


    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvContent: TextView = itemView.findViewById(R.id.tvContent)
    }

    interface MyItemClickListener {
        fun onItemClick(str: String)
    }

    fun setOnItemClickListener(onItemClickListener: MyItemClickListener?) {
        this.myItemClickListener = onItemClickListener
    }
}